<?php

class OrderController extends AbstractAdminController
{
	public function actionList($status = null)
	{
		if($status && strpos($status, ':') !== false)
			$status = explode(':', $status);

        $oOrder = new Order();

        $oRowset = $oOrder->getAll($this->oPager, $status);

		$this->render('list', array(
            'oRowset' => $oRowset,
		    'selectedStatus' => $status,
        ));
	}

    public function actionEditor($id = null)
    {
	    if(!$id)
		    throw new CHttpException(404, 'Not found');

	    $toView = array();

        $oOrder = new Order();
        $oOrderGoods = new OrderGoods();
        $oGoods = new Goods();
        $oPhotos = new Photos();
        $oGoodsProperty = new GoodsProperty();


	    $oRow = $oOrder->findByPk($id);

	    if(!$oRow)
		    throw new CHttpException(404, 'Not found');

	    $toView['oRow'] = $oRow;

	    if($this->_isPost())
	    {
		    $params  = $this->_getPost();
		    $oRow->setAttributes($params);
		    if($oRow->save())
		    {
			    return $this->redirect(Yii::app()->controller->createUrl('/admin/goods/order/list', array()));
		    }
	    }

	    $oOrderGoodsRowset = $oOrderGoods->findAllByAttributes(array(
	                                           'order_id' => $oRow->id
	                                      ));

	    $aOrderGoods = array();
	    foreach($oOrderGoodsRowset as $oRow)
		    $aOrderGoods[$oRow->goods_id] = $oRow;

	    $toView['aOrderGoods'] = $aOrderGoods;

	    $oGoodsRowset = $oGoods->id(array_keys($aOrderGoods))->findAll();
	    $oPhotos->selectMainByRowset($oGoodsRowset);

	    // Select colors
	    $oGoodsProperty = new GoodsProperty();
	    $oGoodsProperty->selectByRowset($oGoodsRowset, '4');

	    // Select sizes
	    $oGoodsProperty->selectByRowset($oGoodsRowset, '3');

	    $toView['oGoodsRowset'] = $oGoodsRowset;
//	    print_r($aOrderGoods); exit;

        $this->render('edit', $toView);
    }

    public function actionDelete($id = null)
    {
	    $oOrder = new Order();

        $oOrder = $oOrder->findByPk($id);

	    if($oOrder && $oOrder->id > 0)
	    {
	        $oOrder->status = 'deleted';
		    $oOrder->save();
	    }

	    $returnUrl = Yii::app()->user->returnUrl;

        if(!empty($_SERVER['HTTP_REFERER']))
            $returnUrl = $_SERVER['HTTP_REFERER'];

        return $this->redirect($returnUrl);
    }

	public function actionProperties($categoryId)
	{
		$toView = array();

		$oCategories = new Categories();
		$aCategoriesIds = $oCategories->getAllParents($categoryId);
		$aCategoriesIds[$categoryId] = $categoryId;
//		    print_r($aCategoriesIds); exit;

		$oCategoryProperty = new CategoryProperty();
		$oProperties = $oCategoryProperty->getByCategoryId($aCategoriesIds);

		$aProperties = array();
		foreach($oProperties as $oProperty)
			$aProperties[$oProperty->id] = $oProperty;

		if(count($aProperties))
		{
			$oPropertyValue = new PropertyValue();
			$oValues = $oPropertyValue->findAllBySql("SELECT * FROM property_value WHERE property_id IN(". implode(', ', array_keys($aProperties)).")");

			foreach($oValues as $oValue)
			{
				if(isset($aProperties[$oValue->property_id]))
					$aProperties[$oValue->property_id]->values[] = $oValue;
			}
		}

		$toView['oProperties'] = $aProperties;

		$this->layout = 'ajax';
		$this->render('properties', $toView);
	}

	public function actionParser()
	{
		if(!$this->_getParam('parser_id') || !$this->_getParam('url'))
			exit;

		$oParser = new Parser();
		$oParser = $oParser->findByPk($this->_getParam('parser_id'));

		if(!$oParser)
			exit;

		$oGrabber = new Grabber();
		$oPriceRow = $oGrabber->getRow($oParser->class, 'PriceParserRow', $this->_getParam('url'));
		$aPriceRow = $oPriceRow->getArray();
//		print_r($aPriceRow); exit;

		echo json_encode($aPriceRow);
		exit;
	}

	public function actionLoadframe($url)
	{
		Yii::import('application.components.grabber.Browser');

		$oBrowser = new Browser();
		$html = $oBrowser->get($url);

		echo $html; exit;
	}

}